package com.cloud.common;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



public class LoginFilter implements Filter {
	
	private static final String LOGISTICS="/logistics/";
	 
     public void init(FilterConfig filterConfig) throws ServletException {
 
     }
 
     public void doFilter(ServletRequest request, ServletResponse response,
             FilterChain chain) throws IOException, ServletException {
         // 获得在下面代码中要用的request,response,session对象
         HttpServletRequest servletRequest = (HttpServletRequest) request;
         HttpServletResponse servletResponse = (HttpServletResponse) response;
         HttpSession session = servletRequest.getSession();
 
         // 获得用户请求的URI
         String path = servletRequest.getRequestURI();
         //System.out.println(path);
         
         // 从session里取管理员id信息
         Integer id =  (Integer) session.getAttribute("id");
         
         // 从session里取手机版管理员pid信息
         Integer pid =  (Integer) session.getAttribute("pid");
         
         String url = servletRequest.getContextPath();
         java.io.PrintWriter out = null;
 
         response.setContentType("text/html;charset=UTF-8");
         /*创建类Constants.java，里面写的是无需过滤的页面
         for (int i = 0; i < Constants.NoFilter_Pages.length; i++) {
 
             if (path.indexOf(Constants.NoFilter_Pages[i]) > -1) {
                 chain.doFilter(servletRequest, servletResponse);
                 return;
             }
     }*/
         /*Cookie[] cookies = servletRequest.getCookies();
         String username = null;
         String psw = null;
         // 遍历所有的 Cookie 寻找 用户帐号信息与登录次数信息
         for(int i=0; cookies!=null&&i<cookies.length; i++){
	         Cookie cookie = cookies[i];
	         if("name".equals(cookie.getName())){
	        	 username = cookie.getValue();
	         }
         }*/
         
         // 登陆页面和静态页面无需过滤
         if(path.indexOf("/login") > -1 || path.indexOf("/js") > -1 || path.indexOf("/styles") > -1 
        		 || path.indexOf("/test") > -1) {
             chain.doFilter(servletRequest, servletResponse);
             return;
         }
         
         if(null != pid && path.indexOf("/mobile") > -1 && SessionManager.isValidUser(pid, session)){
        	 chain.doFilter(request, response);
        	 return;
         }else if(path.indexOf("/mobile") > -1){
        	 out = response.getWriter();
       	  	 out.println("<html>");  
       	  	 out.println("<script>");
        	 out.println("window.open ('"+url+"/theme/mobile/login.jsp','_top')");
        	 out.println("</script>");  
       	  	 out.println("</html>");
       	     return;
         }
 
         
         if(id != null && SessionManager.isValidUser(id, session) && !(path.indexOf("/mobile") > -1)){
        	 
        	 chain.doFilter(request, response);
        	 
         }else{
        	 out = response.getWriter();
       	  	 out.println("<html>");  
       	  	 out.println("<script>");
    		 out.println("window.open ('"+url+"/page/login','_top')");
    		 if(id != null && !SessionManager.isValidUser(id, session)){
    			 out.println("alert('该用户已登录')");
    		 }
       	  	 out.println("</script>");  
       	  	 out.println("</html>");
         }
         
 
     }
 
     public void destroy() {
        
     }
 
 }
